package com.yyl.fmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyl.fmall.product.entity.Product;
import com.yyl.fmall.product.entity.ProductImg;
import com.yyl.fmall.product.entity.ProductParams;
import com.yyl.fmall.product.entity.ProductSku;
import com.yyl.fmall.product.mapper.ProductMapper;
import com.yyl.fmall.product.service.IProductImgService;
import com.yyl.fmall.product.service.IProductParamsService;
import com.yyl.fmall.product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyl.fmall.product.service.IProductSkuService;
import com.yyl.fmall.product.vo.ProductDetailInfoVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author YuanSir
 * @since 2022-11-07
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    
    @Autowired
    private IProductImgService productImgService;
    
    @Autowired
    private IProductSkuService productSkuService;
    
    @Autowired
    private IProductParamsService productParamsService;
    
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Product> getListRecommends() {
        
        //1.创建分页对象
        Page<Product> productPage = new Page<>(1, 3);
        
        this.page(productPage, new LambdaQueryWrapper<Product>()
                .orderByDesc(Product::getCreateTime));
    
        List<Product> productList = productPage.getRecords();
        
        //查询这三个商品对应的图片信息
        Map<String, List<ProductImg>> productImgMap = productImgService
                .list(new LambdaQueryWrapper<ProductImg>()
                .in(ProductImg::getItemId, productList.stream()
                        .map(Product::getProductId)
                        .collect(Collectors.toList())))
                .stream()
                .collect(Collectors.groupingBy(ProductImg::getItemId));
    
        //处理商品对应的图片信息的填充
        productList.forEach(product -> product.setImgs(productImgMap.get(product.getProductId())));
    
        return productList;
    }
    
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ProductDetailInfoVo getDetailInfoById(String productId) {
        
        
        //1. 查询商品表中的对应的商品
        Product product = getOne(new LambdaQueryWrapper<Product>()
                .eq(Product::getProductId, productId));
    
        //2. 查询ProductImg表中对应的商品图片
        List<ProductImg> productImgList = productImgService.list(new LambdaQueryWrapper<ProductImg>()
                .eq(ProductImg::getItemId, productId));
    
        //3. 查询ProductSku表中对应的商品信息
        List<ProductSku> productSkuList = productSkuService.list(new LambdaQueryWrapper<ProductSku>()
                .eq(ProductSku::getProductId, productId)
                .eq(ProductSku::getStatus, 1));
        
        //4. 将查询到的信息放入vo类中
        //创建要返回的vo类并返回
        return new ProductDetailInfoVo(product, productSkuList, productImgList);
        
    }
    
}
